package com.sogoodsoft.web.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Goods;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.crm.GoodsService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Results({ @Result(name = "GOODSCATE", location = "/WEB-INF/content/crm/goodsCate.jsp"),
        @Result(name = "selectGoods", location = "/WEB-INF/content/crm/selectGoods.jsp") })
public class GoodsAction extends CrudActionSupport < Goods > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Long id;

    public Goods entity;

    public Page < Goods > page = new Page < Goods >(WebConstants.PAGE_SIZE);

    @Autowired
    private GoodsService goodsSer;

    @Autowired
    private SysClassService sysClassSer;

    @Override
    public String delete() throws Exception {
        if (id != null) {
            entity = goodsSer.get(id);
            goodsSer.delete(entity);
        }
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String list() throws Exception {
        List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.DESC);
        }

        logger.debug("查询公司商品");
        page = goodsSer.findPage(page, filters);
        return SUCCESS;
    }

    public String selectGoods() throws Exception {
        list();
        return "selectGoods";
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = goodsSer.get(id);
            entity.getCate().getName();
        } else {
            entity = new Goods();
        }

    }

    @Override
    public String save() throws Exception {
        if (entity.getCategory() != null) {
            SysClass sysClass = sysClassSer.get(entity.getCategory());
            entity.setCate(sysClass);

        }
        goodsSer.save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public Goods getModel() {
        return entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Goods getEntity() {
        return entity;
    }

    public void setEntity(Goods entity) {
        this.entity = entity;
    }

    public Page < Goods > getPage() {
        return page;
    }

    public void setPage(Page < Goods > page) {
        this.page = page;
    }

}
